package net.dl.ss.dao.jpa;

import java.io.Serializable;
import java.util.List;

//import javax.persistence.EntityManager;
//import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import net.dl.ss.dao.ibatis.Persistable;

@Repository
@Transactional
public abstract class CommonDaoJPA {
	//@PersistenceContext
	//private EntityManager em;
	
	protected abstract String getSelectQuery();
	
	public <T> T saveEntity(final T entity) {
		/*T merged = this.em.merge(entity);
		this.em.flush();
		((Persistable) entity).setId(((Persistable) merged).getId());
		*/return entity;
	}
	
	public <T> T get(Class<T> c, Serializable id) {
		return null;//this.em.find(c, id);
	}
    
    public void updateEntity(Object entity) {
    	saveEntity(entity);
    }
	
    public void deleteEntityById(Long id) {
		/*Object entity = this.em.find(this.getClass(), id);
		this.em.remove(entity);*/ 
    }
	
    @Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
    public <T> T load(T object, Serializable id) {
    	return null;//(T) this.em.find(this.getClass(), id);
    }
	
    public void deleteEntity(Object entity) {
        deleteEntityById(((Persistable) entity).getId());
    }
    
    @Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
    public <T> List<T> list(Class<T> c) {
    	return null;//this.em.createQuery(getSelectQuery()).getResultList();
    }
    
}
